package com.jysz.certificate.service.system;

import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jysz.certificate.constant.ConfigTypeConstants.ConfigTypes;
import com.jysz.certificate.constant.ConfigTypeConstants.PerformanceConfig;
import com.jysz.certificate.persistence.dao.system.SystemPreferencesDao;
import com.jysz.certificate.persistence.model.core.SystemPreferences;
import com.jysz.certificate.utils.JsonUtil;
import com.jysz.webcore.ssh.service.EntityService;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
@Service
public class SystemPreferencesService extends EntityService<SystemPreferences, Long, SystemPreferencesDao>{

	@Resource
	private SystemPreferencesDao systemPreferencesDao;
	
	public SystemPreferencesDao getEntityDao() {
		return systemPreferencesDao;
	}
    
	
	
	/**
	 * 根据appKey来动态生成页面
	 * @param key
	 * @return
	 */
	public String getSystemPreferencesHtml(String key){
		StringBuilder appHtml =  new StringBuilder();
		SystemPreferences systemPreferences = systemPreferencesDao.findConfigByKey(key);
		JSONObject jb = null;
		if(systemPreferences != null ){//用于修改时数据获取
			if(StringUtils.isNotBlank(systemPreferences.getConfigValue())){
				jb = JSONObject.fromObject(systemPreferences.getConfigValue()); 
			}
			appHtml.append("<input type='hidden' id='appId' value='"+ systemPreferences.getId() +"'>");
		}
		
		if(ConfigTypes.PERFORMANCE_CONFIG.getKey().equals(key)){
			for (PerformanceConfig c : PerformanceConfig.values()) {
				if(jb != null && ConfigTypes.PERFORMANCE_CONFIG.getKey().equals(systemPreferences.getConfigTypeKey())){
					if(jb.containsKey(c.getField())){
						appHtml.append(appCommonHtml(c.getField(), c.getName(), c.getType(), jb.getString(c.getField()), c.getIsEmpty()));
					}else{
						appHtml.append(appCommonHtml(c.getField(), c.getName(), c.getType(), "", c.getIsEmpty()));
					}
				}else{
				    appHtml.append(appCommonHtml(c.getField(), c.getName(), c.getType(), c.getValue(), c.getIsEmpty()));
				}
			}
		}
		
		return appHtml.toString();
	}
	
	/**
	 * 公共的module参数生成页面
	 * @param field 字段
	 * @param name  字段名
	 * @param type  字段类型【 0 text, 1 radio ,2 textfile】
	 * @param value 字段初始化值
	 * @param isEmpty 字段页面提交时是否可为空 【0 否, 1 是】
	 * @return
	 */
	private String appCommonHtml(String field, String name, int type, String value, int isEmpty){
		if(StringUtils.isNotBlank(value)){
			value = value.replace("'", "&#39;");
			value = JsonUtil.JsonEscape(value);
		}
		StringBuilder appHtml =  new StringBuilder();
		appHtml.append("<tr align='center' height='25' bgcolor='#FFFFFF' onMouseMove='javascript:this.bgColor=\"#EDF7D0\";' onMouseOut='javascript:this.bgColor=\"#FFFFFF\";'>");
		appHtml.append("<td>"+ name +":</td><td align='left'>");
		if(type == 0){
			appHtml.append("<input  type='text'  value='"+ value +"' class='appClass' field_name='"+ name +"' is_empty='"+ isEmpty +"'  app_field='"+ field +"'  style='width: 260px;'>");
		}if(type == 1){
			if(value.equals("0")){
				appHtml.append("<input  type='radio' class='appClass' name='"+ field +"' field_name='"+ name +"' app_field='"+ field +"' value='1' >是&nbsp;&nbsp;&nbsp;&nbsp;");
				appHtml.append("<input  type='radio' class='appClass' name='"+ field +"' field_name='"+ name +"' app_field='"+ field +"' value='0' checked>否");
			}else{
				appHtml.append("<input  type='radio' class='appClass' name='"+ field +"' field_name='"+ name +"' app_field='"+ field +"' value='1' checked>是&nbsp;&nbsp;&nbsp;&nbsp;");
				appHtml.append("<input  type='radio' class='appClass' name='"+ field +"' field_name='"+ name +"' app_field='"+ field +"' value='0'>否");
			}
		}if(type == 2){
			appHtml.append("<input  type='text' id="+ field +"  value='"+ value +"' class='input' field_name='"+ name +"' is_empty='"+ isEmpty +"' app_field='"+ field +"'  style='width: 205px;'>");
			appHtml.append("<input type='button' value='浏览...' name='liulan' onclick='SelectTemplets(\""+ field +"\");'>");
		}if(type == 3){
			appHtml.append("<textarea type='textarea' class='appClass' field_name='"+ name +"' is_empty='"+ isEmpty +"'  app_field='"+ field +"' rows='5' style='width: 260px;'>"+ value +"</textarea>");
		}
		appHtml.append("</td></tr>");
		return appHtml.toString();
	}
	
	public Map<String, String> getConfigDataByMap(String key){
		if(StringUtils.isBlank(key)){
			return null;
		}
		SystemPreferences config = systemPreferencesDao.findConfigByKey(key);
		if(config == null){
			return null;
		}
		Map<String, String> dataMap = JsonUtil.getJsonDataToMap(config.getConfigValue());
		return dataMap;
	}

	public SystemPreferences findConfigByConfigType(String appConfigType){
		return systemPreferencesDao.findConfigByKey(appConfigType);
	}
	
}
